﻿define([
    'Xjs/core/util',
    'Xjs/ui/Container',
// Include the modules that register the types of the components of the table
    'Xjs/ui/table/TableHeader',
    'Xjs/ui/table/TableBody',
    'Xjs/ui/table/TableFooter'
],
function (util, Container) {
    'use strict';

    var type = 'table',
    Table = Xjs.getType(type);

    if (Table) { // It has been already loaded
        return Table;
    }

    Table = util.define({
        base: Container,
        construct: function (conf, parent) {
            Table.base.constructor.call(this, conf, parent); // Call the base constructor

            if (conf.caption) {
                this.createCaption(conf.caption);
            }

            if (conf.header) {
                this.createHeader(conf.header);
            }

            if (conf.body) {
                this.createBody(conf.body);
            }

            if (conf.footer) {
                this.createFooter(conf.footer);
            }
        },
        tag: 'table',
        type: type,
        border: true,

        createCaption: function (conf) {
            var caption;

            if (this.id && !conf.id) {
                conf.id = this.id + 'Caption';
            }

            if (util.isString(conf)) {
                caption = Xjs.create('content', {
                    tag: 'caption',
                    content: conf
                }, this);
            }
            else {
                conf.tag = 'caption';
                caption = Xjs.create('control', conf, this);
            }

            this.addChild(caption);
            this.caption = caption;
        },

        getCaption: function () {
            return this.caption;
        },

        createHeader: function (conf) {
            var header = Xjs.create('tableHeader', conf, this);
            this.addChild(header);
            this.header = header;
        },

        getHeader: function () {
            return this.header;
        },

        createBody: function (conf) {
            var body = Xjs.create('tableBody', conf, this);
            this.addChild(body);
            this.body = body;
        },

        getBody: function () {
            return this.body;
        },

        createFooter: function (conf) {
            var footer = Xjs.create('tableFooter', conf, this);
            this.addChild(footer);
            this.footer = footer;
        },

        getFooter: function () {
            return this.footer;
        }
    });

    Xjs.registerType(Table);

    return Table;
});